package com.lic.system.threadLocal;

import com.lic.system.pojo.User;

import java.util.Optional;

/**
 * @author Hewb
 * @ClassName: UserThreadLocal
 * @Description: TODO
 * @date: 2020/7/13 10:05
 * @since JDK 1.8
 */
public class UserThreadLocal {
    /**
     * 如果要保存的数据有多个,则使用Map集合
     */
    private static ThreadLocal<User> userThread = new ThreadLocal<>();

    /**
     * @Descirption 设置当前线程内的uid
     */
    public static void set(User user) {
        userThread.set(user);
    }


    /**
     * @Descirption 获取当前线程的用户对象
     */
    public static User getUser() {
        return Optional.ofNullable(userThread.get()).orElseThrow(RuntimeException::new);
    }

    /**
     * @Descirption 销毁当前线程
     */
    public static void remove() {
        userThread.remove();
    }
}
